package com.linglan.common.config;

import cn.dev33.satoken.interceptor.SaInterceptor;
import cn.dev33.satoken.stp.StpUtil;
import com.linglan.common.config.pro.SASkipUrlProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;

@Configuration
@EnableConfigurationProperties({SASkipUrlProperties.class})
public class SaTokenConfigure implements WebMvcConfigurer {
    @Resource
    private SASkipUrlProperties saSkipUrlProperties;
    @Resource
    private CorsInterceptor corsInterceptor;

    // 注册 Sa-Token 拦截器，打开注解式鉴权功能
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 跨域拦截器需放在最上面
        registry.addInterceptor(corsInterceptor).addPathPatterns("/**");
        // 注册 Sa-Token 拦截器，打开注解式鉴权功能 
        registry.addInterceptor(new MySaInterceptor())
                .addPathPatterns("/**")
                .excludePathPatterns(saSkipUrlProperties.getSkipUrl());
        registry.addInterceptor(new UserInfoInterceptor()).addPathPatterns("/**");
    }

}